--[[
组队副本逻辑
--]]
local ExcelMap = require("excel.Map")
local CopyConfig = ExcelMap.copy
local SceneConfig = ExcelMap.scene
local Timer = require("core.Timer")
local ObjHuman = require("core.ObjHuman")
local Copy = require("copy.CopyL")
local Loss = require("copy.Loss")
local Aim = require("copy.Aim")
local CopyHandler = require("copy.HandlerL")
local InnerDataManager = require("innerdata.InnerDataManagerFL")
local InnerDefine = require("innerdata.Define")
local Log = require("common.Log")

local data = {}
function sendTeamError(human, copyID, err)
   data.copyID = copyID
   data.err = err
   InnerDataManager.Send2WInnerData(human, InnerDefine.INNERDATA_TYPE_COPY_TEAM_ERR, data)
end

function applyCopyRoomTeam(copyID)--, config, uuid)
	if Copy.copyList[copyID] == nil then return end

	for roomID in pairs(Copy.copyList[copyID]) do
		if Copy.roomDatas[copyID][roomID] == nil then
			Copy.roomDatas[copyID][roomID] = {member={}, onlineNum=0,startTime=Timer.now}
			Copy.roomBegin(copyID, roomID)		
			return roomID
		end
	end
	return nil
end

function isEntered(human, copyID, roomID)
  if Copy.copyList[copyID] == nil then return end
	local members = Copy.roomDatas[copyID][roomID].member
	for index, uuid in ipairs(members) do
		if uuid == human._id then
			return true
		end
	end	
	return nil
end

function onEnter(obj, sceneID)
	local copyID = Copy.sceneID2CopyID[sceneID]
	local room = Copy.sceneID2Room[sceneID]
	if Copy.roomDatas[copyID] then
		local roomData = Copy.roomDatas[copyID][room]
		if roomData.clearTime then
			roomData.clearTime = nil			
		end
	end
end

function onLeave(obj, sceneID)
	local copyID = Copy.sceneID2CopyID[sceneID]
	local room = Copy.sceneID2Room[sceneID]
	if Copy.roomDatas[copyID] then
		local roomData = Copy.roomDatas[copyID][room]
		if roomData.onlineNum == 0 then
			roomData.clearTime = Timer.now + Copy.TIME_CLEAR_COPY_ROOM_FINISH
		end
	end
end


function humanBeginCopy(human, copyID, roomID)--, cross)
	local copyConfig = CopyConfig[copyID]
	

	local bEntered = isEntered(human, copyID, roomID)
	if bEntered == true then
		-- 已经进入过了
	else
		-- 还没有进入过
		CopyHandler.incCopyEnterCount(human, copyID)	
		Loss.handler(human, copyConfig, copyID)	-- 扣除进入需要的消耗 
		local members = Copy.roomDatas[copyID][roomID].member
		members[#members + 1] = human._id
	end
	
	Copy.copyHuman2Room[human._id] = roomID
	-- 根据副本目标进度获取进入的地图号
	local mapID, x, y = Aim.getEnterMap(copyID, roomID)
	if mapID == nil then
--		assert(nil, "aim get enter mapid == nil copyID:" .. copyID)
      mapID = copyConfig.mapList[1]
      local sceneConfig = SceneConfig[mapID]
      x = sceneConfig.x
      y = sceneConfig.y
	end
	ObjHuman.enterMap(human, mapID, x, y)
	Log.write(Log.LOGID_OSS_TEAM_COPY,human._id, human.account, human.name,human.db.lv,copyConfig.group,0,copyID)
	return true
end

function humanEndCopy(human)
	local sceneID = human.scene_id
	local copyID = Copy.sceneID2CopyID[sceneID]
	local roomID = Copy.sceneID2Room[sceneID]
	local copyConfig = CopyConfig[copyID]
	
	local backHomeMapId, backHomeX, backHomeY = Copy.getBackHomePoint(human, copyID)
	ObjHuman.enterMap(human,backHomeMapId,backHomeX,backHomeY)
	
	local type = 2
	if Copy.roomDatas[copyID][roomID].finishCode == 1 then
	     type = 1
	end
  Log.write(Log.LOGID_OSS_TEAM_COPY,human._id, human.account, human.name,human.db.lv,copyConfig.group,type,copyID)
end


-- 队伍出人回调
function onDelMemberCB(uuid, copyID, roomID)
	if Copy.copyHuman2Room[uuid] == roomID then
		Copy.copyHuman2Room[uuid] = nil
	end	
	local human = ObjHuman.onlineUuid[uuid]
	if human == nil then return end	
	-- 如果这个玩家在组队副本中 一离队就要把他踢出去
	local sceneID = human.scene_id
	local humanCopyID = Copy.sceneID2CopyID[sceneID]
	local humanRoomID = Copy.sceneID2Room[sceneID]
	if humanRoomID == roomID and humanCopyID == copyID then
		humanEndCopy(human)		
		local members = Copy.roomDatas[copyID][roomID].member
		for i = 1, #members do
			if members[i] == uuid then
				members[i] = members[#members]
				members[#members] = nil
				break
			end
		end
	end
end

-- 队伍解散回调
function onDismissCB(copyID, roomID)
	-- 队伍解散的话 对应的副本也直接清掉
	Copy.roomClear(copyID, roomID)
end
